package leetcode_2301_2400;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class LeeCode_2360 {
    public static void main(String[] args) {
        System.out.println(longestCycle(new int[]{3, 3, 4, 2, 3}));
        System.out.println(longestCycle(new int[]{2,-1,3,1}));
    }
    private static int longestCycle(int[] edges) {
        int n = edges.length, ans = -1;
        int[] time = new int[n];
        for (int i = 0, clock = 1; i < n; i++){
            if (time[i] > 0)
                continue;
            for (int x = i, startTime = clock; x >= 0; x = edges[x]){
                if (time[x] > 0){
                    if (time[x] >= startTime){
                        ans = Math.max(ans, clock - time[x]);
                    }
                    break;
                }
                time[x] = clock++;
            }
        }
        return ans;
    }
}
